package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwbwT;
import cn.e4j.modular.crm.entity.GwbwlT;
import cn.e4j.modular.crm.mapper.GwbwTMapper;
import cn.e4j.modular.crm.mapper.GwbwlTMapper;
import cn.e4j.modular.crm.service.GwbwTService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  行业一级分类 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-01-09
 */
@Service
public class GwbwTServiceImpl extends ServiceImpl<GwbwTMapper, GwbwT> implements GwbwTService {

    @Autowired
    GwbwTMapper gwbwTMapper;

    @Autowired
    GwbwlTMapper gwbwlTMapper;

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwbwlist(Page<Object> page, Dto dto) {

        List<Map<String, Object>> gwbwDtos = this.baseMapper.listGwbwPage(page, dto);

        return gwbwDtos;
    }

    @Transactional(readOnly = true)
    public GwbwT getByGwbw001(String ent,String gwbw001) {
        GwbwT gwbw = this.baseMapper.getByGwbw001(ent, gwbw001);
        return gwbw;
    }

    @Transactional
    @Override
    public void add(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwbwT gwbw = new GwbwT();
        gwbw.setGWBW001(dto.getString("GWBW001"));
        gwbw.setGWBWENT(shiroUser.getDBC());
        gwbw.setGWBWACTI(dto.getString("GWBWACTI"));
        gwbw.setGWBW002(" ");//预留栏位1
        gwbw.setGWBW003(" ");//预留栏位2
        gwbw.setGWBWUSER(shiroUser.getACCOUNT());
        gwbw.setGWBWGRUP(shiroUser.getORG_ID());
        gwbw.setGWBWDATE(LocalDate.now());
        gwbw.setGWBWTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        gwbw.setGWBWORIG(shiroUser.getORG_ID());
        gwbw.setGWBWORIU(shiroUser.getACCOUNT());
        GwbwlT gwbwl = new GwbwlT();
        gwbwl.setGWBWLENT(shiroUser.getDBC());
        gwbwl.setGWBWL001(dto.getString("GWBW001"));
        gwbwl.setGWBWL002(LangUtils.getLang(shiroUser.getLANG()));
        gwbwl.setGWBWL003(dto.getString("GWBWL003"));
        gwbwl.setGWBWL004(dto.getString("GWBWL004"));
        gwbw.insert();
        gwbwl.insert();
    }

    @Transactional
    @Override
    public void edit(Dto dto) {
        gwbwTMapper.updateGwbw(dto);
        gwbwlTMapper.updateGwbwl(dto);
    }

    @Transactional
    @Override
    public void delete(Dto dto) {
        gwbwTMapper.delGwbw(dto);
        gwbwlTMapper.delGwbwl(dto);
    }

    @Transactional
    @Override
    public void delDataBatch(List<GwbwT> gwbwTList) {
        for(GwbwT gwbwT : gwbwTList){
            Dto dto = Dtos.newDto();
            ShiroUser shiroUser = ShiroKit.getUser();
            dto.put("ent",shiroUser.getDBC());
            dto.put("GWBW001",gwbwT.getGWBW001());
            gwbwTMapper.delGwbw(dto);
            gwbwlTMapper.delGwbwl(dto);
        }
    }
}
